/**
 * Project Name:fire-industry-DM-center
 * File Name:ResourceServer.java
 * Package Name:com.firestone.web.center
 * Date:2018年3月27日下午5:23:55
 *
*/

package com.firestone.web.center;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.firestone.bean.dto.PageDTO;
import com.firestone.bean.dto.ReturnDTO;
import com.firestone.bean.dto.resource.ResourceListDTO;
import com.firestone.bean.dto.user.User;
import com.firestone.bean.entity.mysql.primary.ImCenterResource;
import com.firestone.bean.po.resource.SavePO;
import com.firestone.bean.po.resource.UpdatePO;
import com.firestone.bean.vo.BaseListVO;
import com.firestone.bean.vo.resource.ResourceVO;
import com.firestone.converters.center.ConverterResource;
import com.firestone.core.enums.SystemCodeEnum;
import com.firestone.core.exception.FireException;
import com.firestone.service.center.IImCenterResourceService;
import com.firestone.web.BaseServer;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @Description: TODO(系统资源库表 控制器类)
 * @author CAIYJ
 * @date 2018年3月27日 下午5:23:55
 */
@Api(value = "系统资源控制器", tags = { "系统资源接口文档" })
@RestController
@RequestMapping("/resource")
public class ResourceServer extends BaseServer {

    @Autowired
    private IImCenterResourceService imCenterResourceService;

    /**
     * @Description: TODO(资源列表)
     * @param name
     * @param pagePO
     * @return
     * @throws Exception
     * @author CAIYJ
     * @date 2018年3月29日 下午6:29:16
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "资源列表", notes = "资源列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "资源名称", required = false, dataType = "String", paramType = "query") })
    @RequestMapping(value = "/list")
    public ReturnDTO<BaseListVO<ResourceVO>> list(String name,
            @ApiParam PageDTO pagePO) throws Exception {
        Page<ResourceListDTO> page = new Page<>(pagePO.getPage(),
                pagePO.getSize());
        page = imCenterResourceService.listResource(page, name);
        BaseListVO<ResourceVO> vo = ConverterResource.convertListVO(page);
        return ReturnDTO.success(vo);
    }

    /**
     * @Description: TODO(资源保存)
     * @param save
     * @return
     * @author CAIYJ
     * @date 2018年3月29日 上午9:43:03
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "资源保存", notes = "保存资源到系统资源库", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = "/save")
    public ReturnDTO<String> save(@ApiParam SavePO save) {
        ImCenterResource resource = new ImCenterResource();
        BeanUtils.copyProperties(save, resource);
        User user = getUserInfo();
        resource.setCreator(user.getId());
        boolean result;
        try {
            result = imCenterResourceService.insert(resource);
        } catch (DuplicateKeyException e) {
            throw new FireException(
                    SystemCodeEnum.error_duplicated_data.getErrorCode(),
                    "已存在同名资源", e);
        }
        if (result) {
            return ReturnDTO.success("保存成功");
        }
        return ReturnDTO.fail(SystemCodeEnum.error_create_failed);
    }

    /**
     * @Description: TODO(资源修改)
     * @return
     * @author CAIYJ
     * @date 2018年3月29日 下午6:34:41
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "修改资源", notes = "修改资源", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "资源ID", required = false, dataType = "Long", paramType = "path") })
    @RequestMapping(value = "/update/{id}")
    public ReturnDTO<String> update(@PathVariable Long id,
            @ApiParam UpdatePO po) {
        ImCenterResource resource = new ImCenterResource();
        BeanUtils.copyProperties(resource, po);
        User user = getUserInfo();
        resource.setId(id);
        resource.setReviser(user.getId());
        boolean result;
        try {
            result = imCenterResourceService.updateById(resource);
        } catch (DuplicateKeyException e) {
            throw new FireException(
                    SystemCodeEnum.error_duplicated_data.getErrorCode(),
                    "已存在同名资源", e);
        }
        if (result) {
            return ReturnDTO.success("修改成功");
        }
        return ReturnDTO.fail(SystemCodeEnum.error_update_failed);
    }

    /**
     * @Description: TODO(资源删除)
     * @param id
     * @return
     * @author CAIYJ
     * @date 2018年3月29日 下午6:34:26
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "删除资源", notes = "删除资源", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "资源ID", required = false, dataType = "Long", paramType = "path") })
    @RequestMapping(value = "/remove/{id}")
    public ReturnDTO<String> remove(@PathVariable Long id) {
        if (imCenterResourceService.deleteById(id)) {
            return ReturnDTO.success("删除成功");
        }
        return ReturnDTO.fail(SystemCodeEnum.error_delete_failed);
    }

    /**
     * @Description: TODO(权限树状图)
     * @return
     * @author CAIYJ
     * @date 2018年3月29日 下午6:34:13
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "权限树状图", notes = "权限树状图", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "资源ID", required = false, dataType = "Long", paramType = "path") })
    @RequestMapping(value = "/dendrogram/{id}")
    public ReturnDTO<?> dendrogram(@PathVariable Long id) {
        return ReturnDTO.success(null);
    }
}
